package com.zhu.controller;

import com.zhu.pojo.Expend;
import com.zhu.pojo.Income;
import com.zhu.pojo.TransSum;
import com.zhu.pojo.Transform;

import com.zhu.service.IncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class IncomeController {
    @Autowired
    IncomeService incomeService;

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/income/{username}")
    public List<Transform> list(@PathVariable("username") String username){
        return incomeService.getAll(username);
    }


    @CrossOrigin //解决跨域问题
    @PostMapping("/api/queryIn")
    public List<Transform> queryList(@RequestBody Income in) {
        return incomeService.getByCondition(in);
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/saveIncome")
    public String save(@RequestBody Income in) {
        boolean flag = incomeService.save(in);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/updIncome")
    public String upd(@RequestBody Income in) {
        boolean flag = incomeService.upd(in);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/delIncome/{id}")
    public String upd(@PathVariable("id") int id) {
        boolean flag = incomeService.del(id);
        if(flag){
            return "success";
        }else {
            return "false";
        }
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/drawIn")
    public List<Map<Object,Object>> draw(@RequestBody TransSum tr) {

        return incomeService.getNumAndName(tr);
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/adminIn")
    public List<Income> listByAdmin(){
        return incomeService.getAllByAdmin();
    }

    @CrossOrigin //解决跨域问题
    @PostMapping("/api/queryAdminIn")
    public List<Income> queryListByAdmin(@RequestBody Income in) {
        return incomeService.getByAdmin(in);
    }

}

